package com.bestom.bluetoothlock.bluetooth;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

import androidx.core.view.InputDeviceCompat;

import com.bestom.bluetoothlock.util.LogUtils;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

import java.nio.ByteBuffer;

public class BraceletInstructions {
    public static final String INSTRUCTIONS_BLE_STRENGTH = "08";
    public static final String INSTRUCTIONS_BREATHING_LIGHT = "0E";
    public static final String INSTRUCTIONS_CANCEL_FINGER_REGISTER = "03";
    public static final String INSTRUCTIONS_CHANGE_LOCK_STATE = "01";
    public static final String INSTRUCTIONS_DELETE_FINGER = "07";
    public static final String INSTRUCTIONS_FACTORY_RESET = "0D";
    public static final String INSTRUCTIONS_FINGER_AUTO_REGISTER = "15";
    public static final String INSTRUCTIONS_FINGER_MODULE = "0A";
    public static final String INSTRUCTIONS_FINGER_NUMBER = "05";
    public static final String INSTRUCTIONS_FINGER_REGISTER = "02";
    public static final String INSTRUCTIONS_FINGER_REGISTER_RESULT = "09";
    public static final String INSTRUCTIONS_GET_RECORD = "12";
    public static final String INSTRUCTIONS_HEAD = "A1";
    public static final String INSTRUCTIONS_LOCK_STATE = "0F";
    public static final String INSTRUCTIONS_POWER = "06";
    public static final String INSTRUCTIONS_RESET_FINGER = "04";
    public static final String INSTRUCTIONS_SAVE_FINGER = "0B";
    public static final String INSTRUCTIONS_SAVE_FINGER_RESULT = "0C";
    public static final String INSTRUCTIONS_SET_MACHINERY_PARM = "10";
    public static final String INSTRUCTIONS_SET_PASSWORD = "13";
    public static final String INSTRUCTIONS_SYNC_TIME = "11";
    public static final String INSTRUCTIONS_UNBIND = "14";
    private static final String TAG = "BraceletInstructions";

    public static String changeLockStateInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_CHANGE_LOCK_STATE);
    }

    public static String unBindInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_UNBIND);
    }

    public static String getFingerRecordInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_GET_RECORD);
    }

    public static String fingerRegisterInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_FINGER_AUTO_REGISTER);
    }

    public static String fingerRegisterInstructions(String str) {
        return getOneLenthInstructions(INSTRUCTIONS_FINGER_REGISTER, str);
    }

    public static String cancelFingerRegisterInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_CANCEL_FINGER_REGISTER);
    }

    public static String resetFingerInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_RESET_FINGER);
    }

    public static String getFingerNumberInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_FINGER_NUMBER);
    }

    public static String getPowerInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_POWER);
    }

    public static String deleteFingerInstructions(String str) {
        return getOneLenthInstructions(INSTRUCTIONS_DELETE_FINGER, str);
    }

    public static String getBleStrengthInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_BLE_STRENGTH);
    }

    public static String fingerRegisterResultInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_FINGER_REGISTER_RESULT);
    }

    public static String changeFingerModuleInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_FINGER_MODULE);
    }

    public static String saveFingerInstructions(String str) {
        return getOneLenthInstructions(INSTRUCTIONS_SAVE_FINGER, str);
    }

    public static String saveFingerResultInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_SAVE_FINGER_RESULT);
    }

    public static String factoryResetInstructions() {
        return getZeroLenthInstructions(INSTRUCTIONS_FACTORY_RESET);
    }

    public static String breathingLightInstructions(String str) {
        return getOneLenthInstructions(INSTRUCTIONS_SAVE_FINGER, str);
    }

    public static String setPasswordInstructions(String str) {
        return getNLenthInstructions(INSTRUCTIONS_SET_PASSWORD, str.substring(0, 2), str.substring(2, 4), str.substring(4, 6));
    }

    public static String syncTimeInstructions(String str) {
        Log.d("agui", "syn time:" + str);
        return getNLenthInstructions(INSTRUCTIONS_SYNC_TIME, str.substring(0, 2), str.substring(2, 4), str.substring(4, 6), str.substring(6, 8), str.substring(8, 10), str.substring(10, 12), str.substring(12, 14));
    }

    public static String lockStateInstructions() {
        return getZeroLenthInstructions("0F");
    }

    public static String setMachineryParmInstructions(String... strArr) {
        String[] byteDataSort = getByteDataSort(Integer.parseInt(strArr[0]), 2);
        String[] byteDataSort2 = getByteDataSort(Integer.parseInt(strArr[1]), 2);
        String[] byteDataSort3 = getByteDataSort(Integer.parseInt(strArr[2]), 2);
        String[] byteDataSort4 = getByteDataSort(Integer.parseInt(strArr[3]), 2);
        String[] byteDataSort5 = getByteDataSort(Integer.parseInt(strArr[4]), 2);
        String[] byteDataSort6 = getByteDataSort(Integer.parseInt(strArr[5]), 2);
        String[] strArr2 = new String[(byteDataSort.length + byteDataSort2.length + byteDataSort3.length + byteDataSort4.length + byteDataSort5.length + byteDataSort6.length)];
        System.arraycopy(byteDataSort, 0, strArr2, 0, byteDataSort.length);
        System.arraycopy(byteDataSort2, 0, strArr2, 2, byteDataSort2.length);
        System.arraycopy(byteDataSort3, 0, strArr2, 4, byteDataSort3.length);
        System.arraycopy(byteDataSort4, 0, strArr2, 6, byteDataSort4.length);
        System.arraycopy(byteDataSort5, 0, strArr2, 8, byteDataSort5.length);
        System.arraycopy(byteDataSort6, 0, strArr2, 10, byteDataSort6.length);
        return getNLenthInstructions(INSTRUCTIONS_SET_MACHINERY_PARM, strArr2);
    }

    private static String[] getByteDataSort(int i, int i2) {
        if (i2 <= 0) {
            return new String[]{"00"};
        }
        String[] strArr = new String[i2];
        int i3 = 0;
        for (int i4 = 0; i4 < strArr.length; i4++) {
            strArr[i4] = "00";
        }
        String[] analysisData = analysisData(hex10_16(i + ""));
        int length = analysisData.length;
        while (true) {
            length--;
            if (length <= -1) {
                return strArr;
            }
            strArr[i3] = analysisData[length];
            i3++;
        }
    }

    public static String getByteDataString(int i, int i2) {
        if (i2 <= 0) {
            return "00";
        }
        String[] strArr = new String[i2];
        for (int i3 = 0; i3 < strArr.length; i3++) {
            strArr[i3] = "00";
        }
        String[] analysisData = analysisData(hex10_16(i + ""));
        int i4 = 0;
        for (int length = analysisData.length + -1; length > -1; length--) {
            strArr[i4] = analysisData[length];
            i4++;
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (String append : strArr) {
            stringBuffer.append(append);
        }
        return stringBuffer.toString();
    }

    public static String getSendBitmapInstructions(Context context, String str, String str2, String str3) {
        String str4;
        byte[] FormatBMP = FormatBMP(createBitmap(context, str2));
        if (FormatBMP == null) {
            return "";
        }
        if (str3 == null) {
            str3 = "";
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < 11; i++) {
            if (i < str3.length()) {
                stringBuffer.append("0" + str3.charAt(i));
            } else {
                stringBuffer.append("FF");
            }
        }
        byte[] bArr = new byte[11];
        try {
            bArr = Hex.decodeHex(stringBuffer.toString().toCharArray());
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        StringBuffer stringBuffer2 = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer2.append(str);
        String hex10_16 = hex10_16((FormatBMP.length + bArr.length + 1) + "");
        if (hex10_16.length() == 2) {
            stringBuffer2.append("00");
            stringBuffer2.append(hex10_16);
        } else {
            stringBuffer2.append(hex10_16);
        }
        stringBuffer2.append(hex10_16(bArr.length + ""));
        stringBuffer2.append(stringBuffer.toString());
        if (FormatBMP == null || FormatBMP.length == 0) {
            str4 = calculationCRC(stringBuffer2.toString());
        } else {
            str4 = calculationCRC(stringBuffer2.toString(), FormatBMP);
        }
        stringBuffer2.append(new String(Hex.encodeHex(FormatBMP)));
        stringBuffer2.append(str4);
        return stringBuffer2.toString();
    }

    private static String getZeroLenthInstructions(String str) {
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append("00");
        stringBuffer.append("00");
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    private static String getOneLenthInstructions(String str, String str2) {
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append(INSTRUCTIONS_CHANGE_LOCK_STATE);
        stringBuffer.append(str2);
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static String getNLenthInstructions(String str, String... strArr) {
        if (strArr == null || strArr.length < 0) {
            return "";
        }
        StringBuffer stringBuffer = new StringBuffer(INSTRUCTIONS_HEAD);
        stringBuffer.append(str);
        stringBuffer.append(hex10_16(strArr.length + ""));
        for (String append : strArr) {
            stringBuffer.append(append);
        }
        stringBuffer.append(calculationCRC(stringBuffer.toString()));
        proofreadingCRC(stringBuffer.toString());
        return stringBuffer.toString();
    }

    public static String calculationCRC(String str) {
        String str2 = "";
        try {
            byte[] decodeHex = Hex.decodeHex(str.toCharArray());
            if (decodeHex != null) {
                byte b = decodeHex[0];
                for (int i = 1; i < decodeHex.length; i++) {
                    b = (byte) (b + decodeHex[i]);
                }
                byte b2 = (byte) (b & 255);
                str2 = Integer.toHexString(b2) + "";
            }
            if (str2.length() >= 2) {
                return str2;
            }
            return "0" + str2;
        } catch (DecoderException e) {
            e.printStackTrace();
            return str2;
        }
    }

    public static String calculationCRC(String str, byte[] bArr) {
        if (bArr == null || bArr.length <= 0) {
            return "";
        }
        try {
            byte[] decodeHex = Hex.decodeHex(str.toCharArray());
            byte[] bArr2 = new byte[(decodeHex.length + bArr.length)];
            System.arraycopy(decodeHex, 0, bArr2, 0, decodeHex.length);
            System.arraycopy(bArr, 0, bArr2, decodeHex.length, bArr.length);
            byte b = bArr2[0];
            for (int i = 1; i < bArr2.length; i++) {
                b = (byte) (b + bArr2[i]);
            }
            String str2 = Integer.toHexString(b & 255) + "";
            if (str2.length() >= 2) {
                return str2;
            }
            return "0" + str2;
        } catch (DecoderException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean proofreadingCRC(String str) {
        if (str == null || str.length() <= 2) {
            LogUtils.i(TAG, "校验的数据:" + str + "");
            return false;
        }
        char[] charArray = str.substring(0, str.length() - 2).toCharArray();
        String substring = str.substring(str.length() - 2);
        String str2 = "";
        try {
            byte[] decodeHex = Hex.decodeHex(charArray);
            if (decodeHex != null) {
                byte b = decodeHex[0];
                for (int i = 1; i < decodeHex.length; i++) {
                    b = (byte) (b + decodeHex[i]);
                }
                str2 = Integer.toHexString(b & 255) + "";
                if (str2.length() < 2) {
                    str2 = "0" + str2;
                }
            }
            LogUtils.e(TAG, "校对一下:" + str);
            if (substring.equals(str2)) {
                return true;
            }
            return false;
        } catch (DecoderException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String hex10_16(String str) {
        String str2 = "00";
        try {
            str2 = Integer.toHexString(Integer.parseInt(str));
            if (str2.length() % 2 != 0) {
                str2 = "0" + str2;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        LogUtils.i(TAG, "进制转换:" + str2);
        return str2;
    }

    public static String[] analysisData(String str) {
        if (str == null || str.length() % 2 != 0) {
            return null;
        }
        String[] strArr = new String[(str.length() / 2)];
        for (int i = 0; i < strArr.length; i++) {
            int i2 = i * 2;
            strArr[i] = str.substring(i2, i2 + 2);
        }
        return strArr;
    }

    public static String bitTo16(String str) {
        int i;
        if (str == null) {
            return "00";
        }
        int length = str.length();
        if (length != 4 && length != 8) {
            return "00";
        }
        if (length != 8) {
            i = Integer.parseInt(str, 2);
        } else if (str.charAt(0) == '0') {
            i = Integer.parseInt(str, 2);
        } else {
            i = Integer.parseInt(str, 2) + InputDeviceCompat.SOURCE_ANY;
        }
        return hex10_16(i + "");
    }

    private static Bitmap createBitmap(Context context, String str) {
        if (str == null || str.length() <= 0) {
            return null;
        }
        if (str.length() > 6) {
            str = str.substring(0, 6);
        }
        int rgb = Color.rgb(51, 51, 51);
        Paint paint = new Paint();
        paint.setColor(rgb);
        paint.setTextSize((float) 16);
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        int height = rect.height() + 2;
        Bitmap createBitmap = Bitmap.createBitmap((int) paint.measureText(str, 0, str.length()), height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(createBitmap);
        canvas.drawColor(-1);
        canvas.drawText(str, 0.0f, (float) (height - 3), paint);
        canvas.save();
        canvas.restore();
        return createBitmap;
    }

    public static byte[] FormatBMP(Bitmap bitmap) {
        if (bitmap == null) {
            return new byte[]{0, 0};
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] iArr = new int[(width * height)];
        bitmap.getPixels(iArr, 0, width, 0, 0, width, height);
        byte[] addBMP_RGB_888 = addBMP_RGB_888(iArr, width, height);
        ByteBuffer allocate = ByteBuffer.allocate(addBMP_RGB_888.length + 2);
        allocate.put((byte) width);
        allocate.put((byte) height);
        allocate.put(addBMP_RGB_888);
        return allocate.array();
    }

    private static byte[] addBMP_RGB_888(int[] iArr, int i, int i2) {
        int i3;
        int i4 = i;
        int i5 = i4 * i2;
        byte[] bArr = new byte[3];
        if (i5 % 8 != 0) {
            i3 = (i5 / 8) + 1;
        } else {
            i3 = i5 / 8;
        }
        int i6 = i3 % 4;
        if (i6 != 0) {
            i3 += i6;
        }
        byte[] bArr2 = new byte[i3];
        int i7 = 0;
        int i8 = 0;
        int i9 = 0;
        int i10 = 1;
        while (i8 < i5) {
            int i11 = i8 + i4;
            int i12 = i11 - 1;
            Log.i("图像处理", "处理结果:I:" + i8 + "\t W:" + i4 + " \tEND:" + i12 + "\tSTART:" + i8);
            while (i8 <= i12) {
                bArr[i7] = (byte) (iArr[i8] >> i7);
                bArr[1] = (byte) (iArr[i8] >> 8);
                bArr[2] = (byte) (iArr[i8] >> 16);
                String str = "";
                for (byte b : bArr) {
                    String hexString = Integer.toHexString(b & 255);
                    if (hexString.length() == 1) {
                        hexString = "0" + hexString;
                    }
                    str = str + hexString;
                }
                if (i10 > 8) {
                    i9++;
                    i10 = 1;
                }
                if (!str.equals("ffffff")) {
                    bArr2[i9] = (byte) (bArr2[i9] | (1 << (8 - i10)));
                }
                i10++;
                Log.i("TTT", "===buffer_index=" + i9 + "======buffer=" + bArr2[i9]);
                i8++;
                i7 = 0;
            }
            i8 = i11;
        }
        return bArr2;
    }
}
